package com.zhuss.controller.food;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.zhuss.bean.bo.food.product.CreateProductBo;
import com.zhuss.bean.bo.food.product.DownProductBo;
import com.zhuss.bean.bo.food.product.UpdateProductBo;
import com.zhuss.bean.bo.food.product.UpperProductBo;
import com.zhuss.bean.bo.food.product.week.CreateWeekBo;
import com.zhuss.bean.bo.food.product.week.UpdateWeekBo;
import com.zhuss.common.BaseController;
import com.zhuss.common.OperateService;
import com.zhuss.exception.WpsException;
import com.zhuss.food.processor.WeekMenuImport;
import com.zhuss.food.processor.WeekMenuImport2;
import com.zhuss.food.product.ProductOperateServiceExt;
import com.zhuss.util.DateUtil;
import com.zhuss.util.ExcelUtils;


/**
 * 周菜单操作 controller
 * @Author zhushanshuo
   @Date 2020/03/01 16:01
 *
 */
  
 
@RestController
@RequestMapping("/weekMenu")
public class WeekMenuOperateController extends BaseController {
	
	@Autowired
	@Qualifier("productOperateService")
	private OperateService<Void, Object> productOperateService;
	
	@Autowired
	@Qualifier("productOperateServiceExt")
	private ProductOperateServiceExt productOperateServiceExt;
	
	@Autowired
    private WeekMenuImport weekMenuImport;
	
	@Autowired
    private WeekMenuImport2 weekMenuImport2;
	
	/**
	 * 创建
	 * @param bo
	 * @throws WpsException
	 */
	@ResponseBody
    @PostMapping("/create")
	@Transactional(rollbackFor = Exception.class)
    public void create(@RequestBody CreateWeekBo bo) throws WpsException {
		CreateProductBo param = new CreateProductBo();
		BeanUtils.copyProperties(bo, param);
		param.setDate(DateUtil.toDateOnly(bo.getDate()));
		param.setFloor(bo.getFloor());
		param.setProductTime(bo.getProductTime());
		param.setFoodId(bo.getFoodId());
		param.setType(bo.getType());
		param.setStatus(bo.getStatus());
		productOperateService.create(param);
    }
	
	/**
	 * 批量创建
	 * @param bo
	 * @throws WpsException
	 */
	@ResponseBody
    @PostMapping("/batchCreate")
	@Transactional(rollbackFor = Exception.class)
    public void batchCreate(@RequestBody CreateWeekBo bo) throws WpsException {
		List<Long> foodIdList = bo.getFoodIdList();
		for (Long foodId : foodIdList) {
			CreateProductBo param = new CreateProductBo();
			BeanUtils.copyProperties(bo, param);
			param.setDate(DateUtil.toDateOnly(bo.getDate()));
			param.setFloor(bo.getFloor());
			param.setProductTime(bo.getProductTime());
			param.setFoodId(foodId);
			param.setType(bo.getType());
			param.setStatus(bo.getStatus());
			productOperateService.create(param);
		}
		
    }
	
	
	/**
	 * 修改
	 * @param bo
	 * @throws WpsException
	 */
	@ResponseBody
    @PostMapping("/update")
    public void update(@RequestBody UpdateWeekBo bo) throws WpsException {
		UpdateProductBo param = new UpdateProductBo();
		BeanUtils.copyProperties(bo, param);
		param.setDate(DateUtil.toDateOnly(bo.getDate()));
		param.setFloor(bo.getFloor());
		param.setProductTime(bo.getProductTime());
		param.setFoodId(bo.getFoodId());
		param.setType(bo.getType());
		productOperateService.update(param);
    }
	
	/**
	 * 删除
	 * @param bo
	 * @throws WpsException
	 *//*
	@ResponseBody
    @PostMapping("/delete")
    public void delete(@RequestBody DeleteProductBo bo) throws WpsException {
		
		productOperateService.delete(bo);
    }*/
	
	
	/**
	 * 上架
	 * @param bo
	 * @throws WpsException
	 */
	@ResponseBody
    @PostMapping("/upper")
    public void upper(@RequestBody UpperProductBo bo) throws WpsException {
		
		productOperateServiceExt.upper(bo);
    }
	
	
	/**
	 * 下架
	 * @param bo
	 * @throws WpsException
	 */
	@ResponseBody
    @PostMapping("/down")
    public void down(@RequestBody DownProductBo bo) throws WpsException {
		
		productOperateServiceExt.down(bo);
    }
	
	//excel上传
    @RequestMapping(value="/batchUpload", method = RequestMethod.POST)
    public void batchUpload(@RequestParam("file") MultipartFile file, @RequestParam("type") Integer type, 
    		 @RequestParam("week") Integer week,
    		HttpServletResponse response, HttpServletRequest request) throws Exception {
    	List<String[]> resultList = new ArrayList<String[]>();
    	System.out.println(resultList);
		try {
			resultList = ExcelUtils.readExcelFile(file, 0);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		weekMenuImport.process(resultList, response, type, week);
    }
    
    //excel上传
    @RequestMapping(value="/batchUpload2", method = RequestMethod.POST)
    public Object batchUpload2(@RequestParam("file") MultipartFile file, @RequestParam("type") Integer type, 
    		 @RequestParam("week") Integer week,
    		HttpServletResponse response, HttpServletRequest request) throws Exception {
    	List<String[]> resultList = new ArrayList<String[]>();
    	System.out.println(resultList);
		try {
			resultList = ExcelUtils.readExcelFile(file, 0);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Object result = weekMenuImport2.process(file, resultList, type, week);
		return result;
    }
	
}
